<?php
$db = new Database();
$fs = new Functions();
$set_date_time = $fs->set_default_timezone_datetime();
$auth = Auth::getAuth('current_user');
$result = array();
error_reporting(0);
if($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST)){
    
    if(isset($_GET['action'])){
        
        // Apply Created Image
        if($_GET['action'] == "apply_photos"){
            //$file = $_POST['img_file']['name'];
            // Upload Photos
                if($_FILES['img_file']['name'] != ""){
                    $file = $_FILES['img_file'];
                    $filename = md5(md5($auth['id'] . time()));
                    $directory = "images/uploads/brochure/" . $filename;
                    $file_path = $directory  . "/" . $filename;
                    
                    if(!is_dir($directory))
                       mkdir ($directory, 0777);
                    
                    $image_resize = new Image_Resize();
                    //$img = $image_resize->image_reduce($file,$directory,$filename);
                    
                    // Set 1 (Large)
                    $thumbnail0 = $directory."/large_".$filename.".png";
                    image_resize::create_thumbnail($file['tmp_name'],$thumbnail0,"300","300");
                    
                    
                    // Set 2 (Medium)
                    $thumbnail1 = $directory."/medium_".$filename.".png";
                    image_resize::create_thumbnail($file['tmp_name'],$thumbnail1,"150","150");
                    
                    // Set 3 (Small)
                    $thumbnail2 = $directory."/small_".$filename.".png";
                    image_resize::create_thumbnail($file['tmp_name'],$thumbnail2,"50","50");
                    
                    // Set 0 (Set Original)
                    $thumbnail = $directory."/Original_".$filename.".png";
                    //image_resize::create_thumbnail($uploadedfile,$thumbnail,$info[0],$info[1]);
                    move_uploaded_file($file['tmp_name'],$thumbnail);
                    
                    $return = array("notification"  =>  "ok",
                                    "msg"           =>  "Photos was successfully applied.",
                                    "img_name"      =>  "/" . $thumbnail);
                
                    echo json_encode($return);
                }
            
        }
        
        else{
            $this->setLayout();
        }
        
    }else if(isset($_POST['action'])){
        
        if($_POST['action'] == "save_brochure"){
            $bro = $_POST['brochure_json'];
            $brochure_json = json_decode($_POST['brochure_json'],true);
            
            $Brochure_Pagination = new Brochure_Pagination($db,$auth['id']);
            $Brochure_Pagination->page_num	   =   $brochure_json['brochure_page'];
            $Brochure_Pagination->brochure_name   =   $brochure_json['brochure_title'];	
            $Brochure_Pagination->brochure_description   =   $brochure_json['brochure_description'];	
            $Brochure_Pagination->brochure_json   =   $bro;	
            $Brochure_Pagination->brochure_content   =   "";	
            $Brochure_Pagination->user_id   =   $auth['id'];	
            $Brochure_Pagination->status   =   "1";	
            $Brochure_Pagination->date_created   =   $set_date_time['date_time'];	
            $Brochure_Pagination->date_modified   =   $set_date_time['date_time'];	
            $Brochure_Pagination->is_active   =   "1";
            $Brochure_Pagination->save();
            
            $return = array("notification"  =>  "ok",
                            "msg"           =>  "Brochure was successfully saved.","a"=>stripslashes($bro),"b"=>$Brochure_Pagination->brochure_json);
        
            echo json_encode($return);
        }
        
        else{
            $this->setLayout();
        }
    }else{
        $this->setLayout();
    }
}else{
    $this->setLayout();
}

function stripslashes_deep($value)
{
    $string=implode("",explode("\\",$value));
    return stripslashes(trim($string));
    //return $value;
}
?>